#include<iostream>

using namespace std;

const int N = 1e5 + 5;

int a[N], dfn[N], n, idx, mindfn, ans;

int dfs(int x)
{
	dfn[x] = ++idx;
	if (dfn[a[x]]) {
		if (dfn[a[x] >= mindfn]) return idx - mindfn + 1;
		return 0;
	}
	return dfs(a[x]);
}

int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) {
		if (!dfn[a[i]]) {
			mindfn = idx + 1;
			ans = max(ans, dfs(a[i]));
		}
	}
	cout << ans << endl;
	return 0;
}